package com.qidian.by.cms.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qidian.by.cms.mapper.ContentMapper;
import com.qidian.by.cms.service.IContentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qidian.by.pojo.Content;
import com.qidian.by.util.pojo.EsResult;
import com.qidian.by.util.pool.ThreadTools;
import org.junit.platform.commons.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 小可怎么都睡不够
 * @since 2024-05-06
 */
@Service
public class ContentServiceImpl extends ServiceImpl<ContentMapper, Content> implements IContentService {

    @Value("${content-prefix}")
    private String contentPrefix;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public EsResult queryDetailInfo(Content content, Integer page, Integer limit) {
        IPage iPage = new Page<>(limit, page);
        return EsResult.ok("查询成功", this.baseMapper.queryDetailInfo(iPage, content));
    }

    @Override
    public EsResult updateContent(Content content) {
        this.baseMapper.updateContent(content);
        reserve(content);
        return EsResult.ok();
    }

    @Override
    public EsResult getContentListByCatMark(String catMark, Integer count) {
        String key = contentPrefix + catMark + count;
        if (stringRedisTemplate.hasKey(key)) {
            String json = stringRedisTemplate.opsForValue().get(key);
            if (!StringUtils.isBlank(json)) {
                try {
                    List<Content> contents = JSON.parseArray(json, Content.class);
                    return EsResult.ok("查询成功[从缓存得到的数据]", contents);
                } catch (Exception e) {
                    stringRedisTemplate.delete(key);
                }
            }
        }
        List<Content> res = this.baseMapper.getContentListByCatMark(catMark, count);
        if (res.size() == 0) {
            stringRedisTemplate.opsForValue().set(key, "NULL_VALUE");
//            设置存活时间
            stringRedisTemplate.expire(key, 20, TimeUnit.SECONDS);
        } else {
//            将数据缓存起来
            stringRedisTemplate.opsForValue().set(key, JSON.toJSONString(res));
            stringRedisTemplate.expire(key, (int) (Math.random() * 60 + 60), TimeUnit.SECONDS);
        }
        return EsResult.ok("查询成功", res);
    }

    @Override
    public EsResult saveAndReverse(Content content) {
        this.save(content);
        reserve(content);
        return EsResult.ok();
    }

    private void reserve(Content content) {
        if (stringRedisTemplate.keys(contentPrefix + content.getCatMark() + "*").size() > 0) {
            stringRedisTemplate.delete(stringRedisTemplate.keys(contentPrefix + content.getCatMark() + "*"));
        }
        ThreadTools.redisDelayed(1, () -> {
            stringRedisTemplate.delete(stringRedisTemplate.keys(contentPrefix + content.getCatMark() + "*"));
        });
    }


}
